//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    addressList: []
  },

  selectTap: function(e) {
    var id = e.currentTarget.dataset.id;
    wx.request({
      // url: 'https://api.it120.cc/'+ app.globalData.subDomain +'/user/shipping-address/update',
      url: app.globalData.domain + '/wxapp/consignee/' + id + '/',
      header: {
        'Authorization': 'JWT ' + wx.getStorageSync('token'),
        // 'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'PUT',
      data: {
        name: e.currentTarget.dataset.name,
        address: e.currentTarget.dataset.address,
        mobile: e.currentTarget.dataset.mobile,
        is_default: true
      },
      // data: {
      //   token: wx.getStorageSync('token'),
      //   id:id,
      //   isDefault:'true'
      // },
      success: function (res) {
        console.log(res)
        wx.navigateBack({})
      }
    })
  },

  addAddess: function() {
    wx.navigateTo({
      url: "/pages/address-add/index"
    })
  },

  editAddess: function(e) {
    wx.navigateTo({
      url: "/pages/address-add/index?id=" + e.currentTarget.dataset.id
    })
  },

  onLoad: function() {
    console.log('onLoad')
  },
  onShow: function() {
    this.initShippingAddress();
  },
  initShippingAddress: function() {
    var that = this;
    wx.request({
      // url: 'https://api.it120.cc/'+ app.globalData.subDomain +'/user/shipping-address/list',
      url: app.globalData.domain + '/wxapp/consignee/',
      header: {
        'Authorization': 'JWT ' + wx.getStorageSync('token'),
      },
      // data: {
      //   token: wx.getStorageSync('token')
      // },
      success: (res) => {
        // if (res.data.code == 0) {
        if (res.statusCode == 200) {
          that.setData({
            addressList: res.data
          });
          // } else if (res.data.code == 700) {
        } else {
          that.setData({
            addressList: null
          });
        }
      }
    })
  }

})